package com.example.project.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.project.entity.ProjectRole;

/**
 * 项目角色Mapper接口
 * 
 * @author ruoyi
 * @date 2025-11-27
 */
@Mapper
public interface ProjectRoleMapper {

	/**
	 * 查询项目角色
	 * 
	 * @param id 项目角色主键
	 * @return 项目角色
	 */
	@Select("select id, name, code, status, sort_order, create_by, create_time, update_by, update_time, remark "
			+ "from t_project_role where id = #{id}")
	public ProjectRole selectProjectRoleById(Long id);

	/**
	 * 查询项目角色列表
	 * 
	 * @param projectRole 项目角色
	 * @return 项目角色集合
	 */
	@Select("<script>"
			+ "select id, name, code, status, sort_order, create_by, create_time, update_by, update_time, remark "
			+ "from t_project_role where 1=1"
			+ "<if test='projectRole.name != null and projectRole.name != \"\"'> and name like concat('%', #{projectRole.name}, '%')</if>"
			+ "<if test='projectRole.code != null and projectRole.code != \"\"'> and code like concat('%', #{projectRole.code}, '%')</if>"
			+ "<if test='projectRole.status != null and projectRole.status != \"\"'> and status = #{projectRole.status}</if>"
			+ " order by sort_order asc, create_time asc"
			+ "</script>")
	public List<ProjectRole> selectProjectRoleList(@Param("projectRole") ProjectRole projectRole);

	/**
	 * 查询系统角色列表
	 * 
	 * @return 项目角色集合
	 */
	@Select("select id, name, code, status, sort_order, create_by, create_time, update_by, update_time, remark "
			+ "from t_project_role where status = '0' order by sort_order asc, id")
	public List<ProjectRole> selectAllList();

	/**
	 * 查询有效项目角色列表
	 * 
	 * @return 项目角色集合
	 */
	@Select("select id, name, code, status, sort_order, create_by, create_time, update_by, update_time, remark "
			+ "from t_project_role where status = '0' order by sort_order asc, id")
	public List<ProjectRole> selectValidProjectRoleList();

	/**
	 * 新增项目角色
	 * 
	 * @param projectRole 项目角色
	 * @return 结果
	 */
	@Insert("<script>" + "insert into t_project_role" + "<trim prefix='(' suffix=')' suffixOverrides=','>"
            + "<if test='name != null and name != \"\"'>name,</if>"
            + "<if test='code != null and code != \"\"'>code,</if>"
            + "<if test='status != null and status != \"\"'>status,</if>"
            + "<if test='sortOrder != null'>sort_order,</if>"
            + "create_time,"
            + "<if test='createBy != null'>create_by,</if>"
            + "<if test='updateBy != null'>update_by,</if>"
            + "<if test='updateTime != null'>update_time,</if>"
            + "<if test='remark != null'>remark,</if>"
            + "</trim>"
			+ "<trim prefix='values (' suffix=')' suffixOverrides=','>"
            + "<if test='name != null and name != \"\"'>#{name},</if>"
            + "<if test='code != null and code != \"\"'>#{code},</if>"
            + "<if test='status != null and status != \"\"'>#{status},</if>"
            + "<if test='sortOrder != null'>#{sortOrder},</if>"
            + "now(),"
            + "<if test='createBy != null'>#{createBy},</if>"
            + "<if test='updateBy != null'>#{updateBy},</if>"
            + "<if test='updateTime != null'>#{updateTime},</if>"
            + "<if test='remark != null'>#{remark},</if>"
			+ "</trim>" + "</script>")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	public int insertProjectRole(ProjectRole projectRole);

	/**
	 * 修改项目角色
	 * 
	 * @param projectRole 项目角色
	 * @return 结果
	 */
	@Update("<script>" + "update t_project_role" + "<trim prefix='SET' suffixOverrides=','>"
			+ "<if test='name != null and name != \"\"'>name = #{name},</if>"
			+ "<if test='code != null and code != \"\"'>code = #{code},</if>"
			+ "<if test='status != null and status != \"\"'>status = #{status},</if>"
			+ "<if test='sortOrder != null'>sort_order = #{sortOrder},</if>"
			+ "<if test='updateBy != null'>update_by = #{updateBy},</if>"
			+ "update_time = now(),"
			+ "<if test='remark != null'>remark = #{remark},</if>" + "</trim>" 
			+ "where id = #{id}"
			+ "</script>")
	public int updateProjectRole(ProjectRole projectRole);

	/**
	 * 删除项目角色
	 * 
	 * @param id 项目角色主键
	 * @return 结果
	 */
	@Update("delete from t_project_role where id = #{id}")
	public int deleteProjectRoleById(Long id);

	/**
	 * 批量删除项目角色
	 * 
	 * @param ids 需要删除的数据主键集合
	 * @return 结果
	 */
	@Update("<script>" + "delete from t_project_role where id in "
			+ "<foreach item='id' collection='array' open='(' separator=',' close=')'>" + "#{id}" + "</foreach>"
			+ "</script>")
	public int deleteProjectRoleByIds(String[] ids);

	/**
	 * 检查角色名称是否已存在
	 * 
	 * @param name 角色名称
	 * @param id 角色ID（排除自身）
	 * @return 存在数量
	 */
	@Select("<script>" + "select count(1) from t_project_role where name = #{name}"
			+ "<if test='id != 0'> and id != #{id}</if>" + "</script>")
	public int checkRoleName(String name, Long id);

	/**
	 * 统计角色数量
	 * 
	 * @param status 状态
	 * @return 角色数量
	 */
	@Select("select count(*) from t_project_role where status = #{status}")
	Integer selectCountByStatus(String status);
}